package com.tannm.doan.taskmanager.dataview;

import java.util.Calendar;

import com.tannm.doan.taskmanager.agenda.AgendaView;

public class DataViewItem {
	// fields
	public long lID = -1;
	public String sSubject = "";

	private long lStartDate = 0;
	private int iHour = 0;
	private int iMinute = 0;

	public long lRepeatEndOnDate = 0;
	public long lDueDate = 0;

	public int iDurationInMinutes = 0;
	public boolean bAlarm = false;
	public boolean bDone = false;
	public boolean bAllDay = false;
	public long lPriority = -1;
	public int iRepeatType = 0;
	public int iRepeatEvery = 1;
	public int viewMode = AgendaView.viewMode.NONE;

	private long lRepeatDaysBitMask = 0;

	// methods
	public void clear() {
		lRepeatDaysBitMask = 0;
	}

	public boolean isRepeat() {
		return (iRepeatType > 0);
	}

	public boolean useRepeatEndOnDate() {
		return (lRepeatEndOnDate > 0);
	}

	public boolean useDueDate() {
		return (lDueDate > 0);
	}

	public void setStartDate(Calendar calUtilDate, long lStartDate) {
		this.lStartDate = lStartDate;
		calUtilDate.setTimeInMillis(this.lStartDate);
		iHour = calUtilDate.get(Calendar.HOUR_OF_DAY);
		iMinute = calUtilDate.get(Calendar.MINUTE);
	}

	public long getStartDateAsLong() {
		return lStartDate;
	}

	public int getStartHour() {
		if (bAllDay)
			return 0;
		return iHour;
	}

	public int getStartMinute() {
		if (bAllDay)
			return 0;
		return iMinute;
	}

	public int getDuration() {
		return iDurationInMinutes;
	}

	public long getPriority() {
		return lPriority;
	}

	public void setVisibleDay(int iDay) {
		lRepeatDaysBitMask |= 0x01L << iDay;
	}

	public boolean getVisibleDay(int iDay) {
		long bitMask = 0x01L << iDay;
		return ((lRepeatDaysBitMask & bitMask) != 0);
	}

	public long getVisibleDays() {
		return lRepeatDaysBitMask;
	}

	// if bit 0 set -> time occurs for 0 hour, etc...
	public long getTimeDataAsBitMask(int iDay) {
		if (getVisibleDay(iDay)) {
			int iHour = getStartHour();
			long bitHour = 0x01L << iHour;
			return bitHour;
		}
		return 0;
	}

	public int getOverdueDays() {
		long bitMask = 0x01L;
		for (int iDayOffset = 0; iDayOffset < 7; iDayOffset++) {
			if ((lRepeatDaysBitMask & bitMask) == bitMask)
				return iDayOffset;
			bitMask <<= 1;
		}
		return 0;
	}

	public int getTimeKey() {
		return (getStartHour() * 100) + getStartMinute();
	}

	public boolean timeOverdue(int iCurrTimeKey) {
		final int iOverdueDays = getOverdueDays();

		// test if time overdue
		if (iOverdueDays == 0) {
			if (iCurrTimeKey >= getTimeKey())
				return true;
		}

		// some days overdue
		if (iOverdueDays > 0)
			return true;

		return false;
	}

}
